import orderSchema from '../../models/order/order'
import ids from '../../prototype/ids'
import shopModel from "../../models/shopping/shop";
class Order extends ids{
    constructor(){
        super();
        this.addOrder = this.addOrder.bind(this)
    }
    async addOrder(req,res,next){
        // res.header("Access-Control-Allow-Origin", "*");
        try {
            // let rd = parseInt(Math.random()*7)
            // let rd1 = parseInt(Math.random()*7)
            // let city = ["山西","陕西","天津","南京","上海","北京","深圳"]
            // let date = ['111111111','111155444','455464256','22222131213','2142141241','22213123','2012321314']
            let no = await this.setID('order_id');
            let order = new orderSchema({
                total_amount: parseInt(Math.random()*4548),
                total_quantity: parseInt(Math.random()*45),
                unique_id:no,
                user_id:parseInt(Math.random()*99999),
                address_id:parseInt(Math.random()*959),
                status_code: parseInt(Math.random()*2),
                restaurant_id:parseInt(Math.random()*444),
                score:(Math.random()*5).toFixed(2)
            })
            await order.save()
            res.send({status:1})
        }catch (e) {
            res.send({status:0,err:"ERR_GET_ADMIN_COUNT"})
        }
    }
    async allOrder(req,res,next){
        // res.header("Access-Control-Allow-Origin", "*");
        try {
            let result1 = await orderSchema.find()
            res.send({status:1,count:result1.length})
        }catch (e) {
            res.send({status:0,count:0,err:"ERR_GET_ADMIN_COUNT"})
        }
    }
    async getAllOrder(req,res,next){
        let count = await orderSchema.count();
        let shopName = [];
        console.log(req.query)
        let {limit = 10,offset = 0} = req.query;
        let result = await orderSchema.find({},"-_id").sort({id:1}).limit(Number(limit)).skip(Number(offset));
        await result.forEach(async (item) => {
            let shop = await shopModel.findOne({id:item.restaurant_id})
            // console.log(shop.name)
            // shopName.push()
        })
        // console.log(shopName)

        res.send({
            count,
            result
        })
    }
    async orderCount(req,res,next){
        const date=req.params.date;
        if(!date){
            res.send({
                status: 0,
                type: 'ERROR_PARAMS',
                message: '参数错误'
            });
            return
        }
        try{
            const count = await OrderModel.find({
                formatted_created_at: eval('/^' + date + '/gi')
            }).count();
            res.send({
                status: 1,
                count: count
            })
        }catch (e) {
            // console.log(e)
            res.send({
                status: 0,
                type: "ERROR_GET_ORDER_REGISTE_COUNT",
                message: "获取当天订单失败"
            })
        }
    }
    async allOrderCount(req,res,next){
        try{
            const count = await OrderModel.count();
            res.send({
                status: 1,
                count: count
            })
        }catch (e) {
            res.send({
                status: 0,
                type: "ERROR_GET_ORDER_REGISTE_COUNT",
                message: "获取订单失败"
            })
        }
    }
}
export default new Order()
// import orderSchema from '../../models/order/order'
// import dtime from 'time-formater'
// import ids from '../../prototype/ids'
// import shopModel from "../../models/shopping/shop";
// class Order extends ids{
//     constructor(){
//         super();
//         this.addOrder = this.addOrder.bind(this)
//     }
//     async addOrder(req,res,next){
//         // res.header("Access-Control-Allow-Origin", "*");
//         try {
//             // let rd = parseInt(Math.random()*7)
//             // let rd1 = parseInt(Math.random()*7)
//             // let city = ["山西","陕西","天津","南京","上海","北京","深圳"]
//             // let date = ['111111111','111155444','455464256','22222131213','2142141241','22213123','2012321314']
//             let no = await this.setID('order_id');
//             let order = new orderSchema({
//                 total_amount: parseInt(Math.random()*4548),
//                 total_quantity: parseInt(Math.random()*45),
//                 unique_id:no,
//                 user_id:parseInt(Math.random()*99999),
//                 address_id:parseInt(Math.random()*959),
//                 status_code: parseInt(Math.random()*2),
//                 restaurant_id:parseInt(Math.random()*444),
//                 score:(Math.random()*5).toFixed(2)
//             })
//             await order.save()
//             res.send({status:1})
//         }catch (e) {
//             res.send({status:0,err:"ERR_GET_ADMIN_COUNT"})
//         }
//     }
//     async allOrder(req,res,next){
//         // res.header("Access-Control-Allow-Origin", "*");
//         try {
//             let result1 = await orderSchema.find()
//             res.send({status:1,count:result1.length})
//         }catch (e) {
//             res.send({status:0,count:0,err:"ERR_GET_ADMIN_COUNT"})
//         }
//     }
//     async getAllOrder(req,res,next){
//         let count = await orderSchema.count();
//         let shopName = [];
//         console.log(req.query)
//         let {limit = 10,offset = 0} = req.query;
//         let result = await orderSchema.find({},"-_id").sort({id:1}).limit(Number(limit)).skip(Number(offset));
//         await result.forEach(async (item) => {
//             let shop = await shopModel.findOne({id:item.restaurant_id})
//             // console.log(shop.name)
//             // shopName.push()
//         })
//         console.log(shopName)
//
//         res.send({
//             count,
//             result
//         })
//     }
// }
// export default new Order()